package com.cdplife.ci.modular.system.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.cdplife.ci.core.common.annotion.BussinessLog;
import com.cdplife.ci.core.common.annotion.FormToken;
import com.cdplife.ci.core.common.annotion.Permission;
import com.cdplife.ci.core.common.constant.dictmap.AccessoryDict;
import com.cdplife.ci.core.common.exception.BizExceptionEnum;
import com.cdplife.ci.core.common.ftp.UploadResult;
import com.cdplife.ci.modular.system.model.Accessory;
import com.cdplife.ci.modular.system.service.IAccessoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 控制器
 *
 * @author jers
 * @Date 2018-11-14 11:27:12
 */
@Controller
@RequestMapping("/uploadFile")
public class UploadFileController extends BaseController {

    @Autowired
    private IAccessoryService accessoryService;

    private String PREFIX = "/system/common/";
    @BussinessLog(value = "附件上传", key = "accessoryTable,fileName", dict = AccessoryDict.class)
    @RequestMapping(value = "/{accessoryTable}/{refId}/{accessoryType}/{fileName}")
    @ResponseBody
    public Object index(HttpServletRequest request,
                        @PathVariable("accessoryTable") String accessoryTable,
                        @PathVariable("refId") Integer refId,
                        @PathVariable("accessoryType") Integer accessoryType,
                        @PathVariable("fileName") String fileName
    ) {
        try {
            UploadResult uploadResult = new UploadResult(request,accessoryTable,refId,accessoryType);
            return accessoryService.saveAccessory(uploadResult,accessoryTable,refId,accessoryType);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    }
    @RequestMapping(value = "delete/{accessoryId}")
    @ResponseBody
    public Object delete(@PathVariable("accessoryId") Integer accessoryId) {
        try {
            return accessoryService.deleteAccessory(accessoryId);
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    }

    /**
     * @brief 跳转到添加附件
     * @param accessoryTable 表名
     * @param refId 主键ID
     * @param accessoryType 附件类型
     * @param model
     * @return
     */
    @RequestMapping("/accessory/{accessoryTable}/{refId}/{accessoryType}")
    public String accessory(@PathVariable String accessoryTable,
                                     @PathVariable Integer refId,
                                     @PathVariable Integer accessoryType,
                                     Model model) {
        List<Accessory> accessorys = accessoryService.getAccessoryList(accessoryTable,refId,accessoryType);
        model.addAttribute("accessoryTable",accessoryTable);
        model.addAttribute("refId",refId);
        model.addAttribute("accessoryType",accessoryType);
        model.addAttribute("accessorys",accessorys);
        return PREFIX + "accessory.html";
    }
}
